package com.john.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.john.seckill.exception.GlobalException;
import com.john.seckill.mapper.UserMapper;
import com.john.seckill.pojo.User;
import com.john.seckill.service.IUserService;
import com.john.seckill.utils.CookieUtil;
import com.john.seckill.utils.MD5Util;
import com.john.seckill.utils.UUIDUtil;
import com.john.seckill.vo.LoginVo;
import com.john.seckill.vo.ResultBean;
import com.john.seckill.vo.ResultBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  服务实现类
 * </p>
 * seckill
 * @author john
 * @since 2021-05-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    /**
     * 登录
     * @param loginVo
     * @param request
     * @param response
     * @return
     */
    @Override
    public ResultBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {

        final String mobile = loginVo.getMobile();
        final String password = loginVo.getPassword();
        //if(StringUtils.isEmpty(mobile)|| StringUtils.isEmpty(password)){
        //    return ResultBean.error(ResultBeanEnum.LOGIN_ERROT);
        //}
        //if(!ValidatorUtil.isMobile(mobile)){
        //    return ResultBean.error(ResultBeanEnum.MOBILE_ERROT);
        //}
        //根据手机号获取用户
        final User user = userMapper.selectById(mobile);
        if(null == user){
            throw  new GlobalException(ResultBeanEnum.LOGIN_ERROT);
        }
        //校验密码
        if(!MD5Util.formPassToDBPass(password,user.getSalt()).equals(user.getPassword())){
            throw  new GlobalException(ResultBeanEnum.LOGIN_ERROT);
        }
        // 生成cookie
        final String ticket = UUIDUtil.uuid();

        //request.getSession().setAttribute(ticket,user);
        //用户信息放入redis,达到分布式session的效果
        redisTemplate.opsForValue().set("user:"+ticket,user);

        CookieUtil.setCookie(request,response,"userTicket",ticket);
        return ResultBean.success(ticket);
    }

    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if(StringUtils.isEmpty(userTicket)){
            return null;
        }
        final User user = (User) redisTemplate.opsForValue().get("user:" + userTicket);
        if(user!=null){
            CookieUtil.setCookie(request,response,"userTicket",userTicket);
        }
        return user;
    }

    /**
     * 更改密码
     *
     * @param userTicket
     * @param password
     * @param request
     * @param response
     * @return
     */
    @Override
    public ResultBean updatePassword(String userTicket, String password, HttpServletRequest request, HttpServletResponse response) {
        User user = getUserByCookie(userTicket,request,response);
        if(user==null){
            throw new GlobalException(ResultBeanEnum.MOBILE_NOT_EXIST);
        }
        user.setPassword(MD5Util.inputPassToDBPass(password,user.getSalt()));
        int result = userMapper.updateById(user);
        if(1==result){
            redisTemplate.delete("user:"+userTicket);
            return ResultBean.success();
        }
        return ResultBean.error(ResultBeanEnum.PASSWORD_UPDATE_FAIL);
    }

}
